﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using AutoMapper;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Sautom.ClientComunication;
using Sautom.ClientComunication.CommandService;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Client.Controls.ViewModels;

namespace Sautom.Modules.Client.ViewModels
{
    public sealed class ClientEditViewModel : NavigationAwareNotificationObject
    {
        #region Constructor
        public ClientEditViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            QueriesServiceClient = serviceFactory.GetQueriesService();
            CommandServiceClient = serviceFactory.GetCommandsService();

            BackCommand = new DelegateCommand(ExecuteBackCommand);
            SaveCommand = new DelegateCommand(ExecuteSaveCommand);
        }

        #endregion

        #region Commands
        public DelegateCommand BackCommand { get; private set; }
        public DelegateCommand SaveCommand { get; private set; }

        private void ExecuteSaveCommand()
        {
            //todo make it pretty
            bool isError = false;
            if (string.IsNullOrWhiteSpace(this.ClientInfoEditorViewModel.FirstName))
            {
                isError = true;
                this.ClientInfoEditorViewModel.IsFirstNameError = true;
            }
            else
            {
                this.ClientInfoEditorViewModel.IsFirstNameError = false;
            }

            if (string.IsNullOrWhiteSpace(this.ClientInfoEditorViewModel.MiddleName))
            {
                isError = true;
                this.ClientInfoEditorViewModel.IsMiddleNameError = true;
            }
            else
            {
                this.ClientInfoEditorViewModel.IsMiddleNameError = false;
            }

            if (string.IsNullOrWhiteSpace(this.ClientInfoEditorViewModel.LastName))
            {
                isError = true;
                this.ClientInfoEditorViewModel.IsLastNameError = true;
            }

            else
            {
                this.ClientInfoEditorViewModel.IsLastNameError = false;
            }

            if (isError)
            {
                return;
            }
            Mapper.CreateMap<ClientInfoEditorViewModel, ClientEditDtoInput>();
            var data = Mapper.Map<ClientEditDtoInput>(this.ClientInfoEditorViewModel);

            CommandServiceClient.EditOrAddClient(ClientInfoEditorViewModel.Id, data);

            ExecuteBackCommand();
        }

        private void ExecuteBackCommand()
        {
            Uri uri;
            if (ClientInfoEditorViewModel.Id == Guid.Empty)
            {
                uri = new Uri(PathProvider.ClientIndex, UriKind.Relative);
            }
            else
            {
                var uriQuery = new UriQuery { { "clientId", ClientInfoEditorViewModel.Id.ToString() } };
                uri = new Uri(PathProvider.ClientDetails + uriQuery, UriKind.Relative);
            }


            RegionManager.RequestNavigate(RegionProvider.MainRegion, uri);
        }
        #endregion

        #region Properties
        private IRegionManager RegionManager { get; set; }
        private IQueriesService QueriesServiceClient { get; set; }
        private ICommandService CommandServiceClient { get; set; }

        public ClientInfoEditorViewModel ClientInfoEditorViewModel { get; set; }
        #endregion

        #region INavigationAware implementation
        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            // Not refresh every time the view is loaded
            if (ClientInfoEditorViewModel != null) return;

            // Initial load - Load based on ID passed in
            string id = navigationContext.Parameters["clientId"];

            ClientEditDtoOutput client = QueriesServiceClient.GetClientForEdit(!string.IsNullOrWhiteSpace(id) ? Guid.Parse(id) : Guid.Empty);
            Mapper.CreateMap<ClientEditDtoOutput, ClientInfoEditorViewModel>();
            ClientInfoEditorViewModel = Mapper.Map<ClientInfoEditorViewModel>(client) ?? new ClientInfoEditorViewModel() { BirthDate = DateTime.Now.AddYears(-18) };
        }
        #endregion
    }
}
